#include "Camera.h"

#include "math.h"
#include "stdlib.h"

Camera::Camera():
	posX(0), 
	posY(0), 
	widthUnits(50), 
	heightUnits(0), 
	targetX(0), 
	targetY(0), 
	stepSize(5), 
	shakingCounter(0), 
	shakingRadius(0), 
	lastRandX(0), 
	lastRandY(0) {}

void Camera::shake(double seconds, float radius){
	shakingCounter = seconds;
	shakingRadius = radius;
}

void Camera::update(double dt){
	posX -= lastRandX;
	posY -= lastRandY;
	float difX = 0;//targetX - posX;
	float difY = targetY - posY;
	float dist = sqrtf(difX*difX+difY*difY);
	float angle = difX;
	angle /= dist;
	angle = acosf(angle);

	if(difY < 0.0f){
		angle *= -1;
	}

	if(dist >= stepSize*dt){
		posX += cosf(angle)*stepSize*dist*0.5f*(float)dt;
		posY += sinf(angle)*stepSize*dist*0.5f*(float)dt;
	}else{
		//posX = targetX;
		posY = targetY;
	}

	if(shakingCounter!=0){
		lastRandX = (((rand()%100)-50)/100.0f)*shakingRadius;
		lastRandY = (((rand()%100)-50)/100.0f)*shakingRadius;

		posX += lastRandX;
		posY += lastRandY;

		shakingCounter-=dt;
		if(shakingCounter<=0){
			shakingCounter=0;
			lastRandX = 0;
			lastRandY = 0;
		}
	}


	
}